package com.example.user.contrller;

import com.example.common.util.R;
import com.example.model.user.dto.LoginDto;
import com.example.model.user.pojo.User;
import com.example.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Tag(name = "用户模块")
@RestController
@RequestMapping("/api/v1/")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login/login_auth")
    @Operation(summary = "用户登录")
    public R login(@RequestBody LoginDto loginDto) {
        Map<String, Object> map = userService.login(loginDto);
        User user = (User) map.get("user");
        String token = (String) map.get("token");
        if (user != null && token != null) {
            map.put("user", user);
            map.put("token", token);
            return R.ok("用户登录成功").data(map);
        } else {
            map.put("token", token);
            return R.ok("游客登录成功").data(map);
        }
    }
}
